
# Top-level Makefile.
#
# NOTE: To build in a subdirectory, please use 'make C=dir' instead of 'make -C dir'
#


BUILDNAME ?= x86

##########################################################################
# Setup default build paths
##########################################################################

M_DIR_ROOT       ?= $(PWD)
M_DIR_SRC        ?= $(M_DIR_ROOT)/src
M_DIR_INCLUDE    ?= $(M_DIR_SRC)/include
M_DIR_BUILD      ?= $(M_DIR_ROOT)/build/$(BUILDNAME)
M_DIR_OBJ        ?= $(M_DIR_ROOT)/obj/$(BUILDNAME)
M_DIR_DEP        ?= $(M_DIR_OBJ)/dep
M_DIR_TMP        ?= $(M_DIR_OBJ)/tmp
M_DIR_UTILS      ?= $(M_DIR_ROOT)/utils

export                   \
	M_DIR_ROOT         \
	M_DIR_SRC          \
	M_DIR_INCLUDE      \
	M_DIR_BUILD        \
	M_DIR_OBJ          \
	M_DIR_DEP          \
	M_DIR_TMP          \
	M_DIR_UTILS        \

##########################################################################
# Setup default Ailes paths
##########################################################################

M_P_SYSTEM    ?= system
M_P_PROGRAMS  ?= programs
M_P_HELPFILES ?= programs/helpfiles
M_P_LIBRARIES ?= system/libraries
M_P_BOOT      ?= system/boot

export                   \
	M_P_SYSTEM         \
	M_P_PROGRAMS       \
	M_P_HELPFILES      \
	M_P_LIBRARIES      \
	M_P_BOOT           \

##########################################################################
# 
##########################################################################

include $(M_DIR_ROOT)/Makefile.include

MAKEFILES += $(M_DIR_ROOT)/Makefile.include
MAKEFLAGS += --no-print-directory

M_MAKEBUILD = $(M_DIR_ROOT)/Makefile.build
export M_MAKEBUILD

ifdef C

GOALS = $(if $(MAKECMDGOALS),$(MAKECMDGOALS),all)

$(GOALS):
	$(MAKE) -C $(C) $(MAKECMDGOALS)

.PHONY: $(GOALS)

else

all: mkdirs
	$(Q)cp COPYING.txt $(M_DIR_BUILD)/$(M_P_SYSTEM)/
	$(Q)make -C dist
	$(Q)make -C utils
	$(Q)make -C src

clean:
	rm -f *~ core
	make -C dist clean
	make -C utils clean
	make -C src clean


image-cd: mkdirs
	$(Q)$(M_DIR_UTILS)/image-cd.sh

image-floppy: mkdirs
	$(Q)$(M_DIR_UTILS)/image-floppy.sh

image-cd-r: mkdirs
	$(Q)$(M_DIR_UTILS)/image-cd.sh -r

image-floppy-r: mkdirs
	$(Q)$(M_DIR_UTILS)/image-floppy.sh -r

check-tree:
	$(Q)$(M_DIR_UTILS)/check-tree.sh 

fix_sh:
	find . -type f -a ! -name \*.sh -exec chmod -x {} \;

mkdirs:
	$(Q)mkdir -p $(M_DIR_BUILD)/$(M_P_SYSTEM)
	$(Q)mkdir -p $(M_DIR_BUILD)/$(M_P_SYSTEM)
	$(Q)mkdir -p $(M_DIR_OBJ)
	$(Q)mkdir -p $(M_DIR_TMP)

endif
